package com.mask.im.server.config;

import com.mask.im.server.service.ServiceIdManager;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.listener.ContainerProperties;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * Kafka消费者配置
 * @author jian
 */
@Configuration
public class KafkaConsumerConfig {

    @Value("${im.kafka.bootstrap-servers:localhost:9092}")
    private String bootstrapServers;

    @Value("${im.kafka.consumer.group-id-prefix:im-server-group-}")
    private String groupIdPrefix;

    @Autowired(required = false)
    private ServiceIdManager serviceIdManager;

    @PostConstruct
    public void validateConfig() {
        if (bootstrapServers == null || bootstrapServers.trim().isEmpty()) {
            throw new IllegalStateException("Kafka bootstrap servers 配置不能为空");
        }
        System.out.println("Kafka消费者配置验证通过，bootstrap servers: " + bootstrapServers);
    }

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        
        // 延迟获取nodeId，避免循环依赖
        String nodeId = serviceIdManager != null ? serviceIdManager.getNodeId() : "default-node";
        props.put(ConsumerConfig.GROUP_ID_CONFIG, groupIdPrefix + nodeId);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
        props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 50);
        return new DefaultKafkaConsumerFactory<>(props);
    }

    @Bean("kafkaListenerContainerFactory")
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.setConcurrency(3);
        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
        return factory;
    }
}
    